package servlet;

import dao.DaoFactory;
import entity.MyFile;
import utils.UploadUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//上传文件
@WebServlet("/upFile")
public class UpFileServlet extends HttpServlet {

    private static final long serialVersionUID = 9156259953841963875L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //MAP目的:携带数据(form表单中的字段)
        Map<String,String> map=new HashMap<String,String>();
        //目的:携带数据,向sercie,dao传递
        MyFile myfile = new MyFile();
        String s_id = req.getParameter("s_id");
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date date = new Date(System.currentTimeMillis());
        String f_time =formatter.format(date);
        myfile.setS_id(s_id);
        myfile.setF_time(f_time);
        try {
            //以下三行代码功能: 通过request.getInputStream();获取到请求体的全部内容
            //进行解析,将每对分割线中的内容封装在了FileItem对象中
            DiskFileItemFactory fac=new DiskFileItemFactory();
            ServletFileUpload upload=new ServletFileUpload(fac);
            List<FileItem> list=upload.parseRequest(req);
            //4_遍历集合
            for (FileItem item : list) {
                if(item.isFormField()){
                    //5_如果当前的FileItem对象是普通项
                    //将普通项上name属性的值作为键,将获取到的内容作为值,放入MAP中
                    map.put(item.getFieldName(), item.getString("utf-8"));
                }else{

                    //获取到原始的文件名称
                    String oldFileName=item.getName();
                    map.put("f_name", oldFileName);
                    //获取到要保存文件的名称
                    String newFileName= UploadUtil.getUUIDName(oldFileName);
                    //通过FileItem获取到输入流对象,通过输入流可以获取到图片二进制数据
                    InputStream is=item.getInputStream();

                    //tomcat的安装目录
                    //D:\Tomcat\apache-tomcat-9.0.56\项目名\file
                    String realPath = getServletContext().getRealPath("/file/");

                    realPath = realPath+s_id.toString();//每个考试，创建一个文件夹，e_id
                    File dirFile = new File(realPath);
                    if(!dirFile.exists()) {
                        dirFile.mkdirs();
                    }
                    //在服务端创建一个空文件(后缀必须和上传到服务端的文件名后缀一致)
                    File file=new File(realPath,newFileName);
                    if(!file.exists()){
                        file.createNewFile();
                    }
                    System.out.println("file:ok"+file);
                    //建立和空文件对应的输出流
                    OutputStream os=new FileOutputStream(file);
                    //将输入流中的数据刷到输出流中
                    //释放资源
                    IOUtils.copy(is, os);
                    IOUtils.closeQuietly(is);
                    IOUtils.closeQuietly(os);
                    System.out.println("file2:ok"+file);
                    //向map中存入一个键值对的数据(****这里就是form提交文件名的那个字段)
                    map.put("f_path", "/file/"+s_id+"/"+newFileName);
                }
            }
            //7_利用BeanUtils将MAP中的数据填充到exam对象上
            BeanUtils.populate(myfile, map);

            //8_调用servcie_dao将exam上携带的数据存入数据库
            DaoFactory.getInstance().getFileDao().add(myfile);
            resp.sendRedirect("myfile?method=list&msg= Upload File sucessful!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
